package com.example.Windturbine.Controller;

import com.alibaba.excel.EasyExcel;
import com.example.Windturbine.Controller.ViewObject.Excel.QAExcel;
import com.example.Windturbine.Controller.ViewObject.Excel.TransportExcel;
import com.example.Windturbine.Dao.ChickenDOMapper;
import com.example.Windturbine.DataObject.TransportDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Listener.ExceTransportListener;
import com.example.Windturbine.Listener.ExcelChickenListener;
import com.example.Windturbine.Listener.ExcelQAListener;
import com.example.Windturbine.Response.CommonResult;
import com.example.Windturbine.Service.Model.TransportModel;
import com.example.Windturbine.Service.TransportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import static com.example.Windturbine.Controller.BaseController.CONTENT_TYPE_FORMED;

/**
 * @Author 丁永新
 * @Date 2021/12/16
 */
@Slf4j
@Controller("transport")
@RequestMapping("/transport")
@CrossOrigin(allowCredentials="true", allowedHeaders = "*")
public class TransportController {
    @Autowired
    TransportService transportService;

    @Autowired
    RedisTemplate redisTemplate;

    @Resource
    ChickenDOMapper chickenDOMapper;

    @RequestMapping("/get")
    @ResponseBody
    public CommonResult getAllTransport(@RequestParam(name="id")Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        List<TransportDO> res=transportService.getAll(id);
        return CommonResult.create(res);
    }

    @RequestMapping(value = "/submit",method = {RequestMethod.POST},consumes={CONTENT_TYPE_FORMED})
    @ResponseBody
    public CommonResult submit(@RequestParam(name="tran_name")String tran_name,
                               @RequestParam(name="tran_tool")String tran_tool,
                               @RequestParam(name="tran_type")String tran_type,
                               @RequestParam(name="tran_unit")String tran_unit,
                               @RequestParam(name="tran_region")String tran_region,
                               @RequestParam(name="tran_origin")String tran_origin,
                               @RequestParam(name="tran_comments")String tran_comments,
                               @RequestParam(name="coal") BigDecimal coal,
                               @RequestParam(name="oil")BigDecimal oil,
                               @RequestParam(name="gas")BigDecimal gas,
                               @RequestParam(name="co")BigDecimal co,
                               @RequestParam(name="co2")BigDecimal co2,
                               @RequestParam(name="so2")BigDecimal so2,
                               @RequestParam(name="nox")BigDecimal nox,
                               @RequestParam(name="ch4")BigDecimal ch4,
                               @RequestParam(name="h2s")BigDecimal h2s,
                               @RequestParam(name="cod")BigDecimal cod,
                               @RequestParam(name="hcl")BigDecimal hcl,
                               @RequestParam(name="nh3")BigDecimal nh3,
                               @RequestParam(name="cfc11")BigDecimal cfc11,
                               @RequestParam(name="cfc12")BigDecimal cfc12,
                               @RequestParam(name="cfc113")BigDecimal cfc113,
                               @RequestParam(name="level")Integer level) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {

        TransportModel materialModel=new TransportModel();
        materialModel.setName(tran_name);
        materialModel.setTool(tran_tool);
        materialModel.setType(tran_type);
        materialModel.setUnit(tran_unit);
        materialModel.setRegion(tran_region);
        materialModel.setOrigin(tran_origin);
        materialModel.setComments(tran_comments);
        materialModel.set煤(coal);
        materialModel.set天然气(gas);
        materialModel.set石油(oil);
        materialModel.setCo(co);
        materialModel.setCo2(co2);
        materialModel.setSo2(so2);
        materialModel.setNox(nox);
        materialModel.setCh4(ch4);
        materialModel.setH2s(h2s);
        materialModel.setCod(cod);
        materialModel.setHcl(hcl);
        materialModel.setNh3(nh3);
        materialModel.setCfc11(cfc11);
        materialModel.setCfc12(cfc12);
        materialModel.setCfc113(cfc113);
        materialModel.setLevel(level);

        transportService.submitSingle(materialModel);

        return CommonResult.create(null);
    }

    @RequestMapping(value = "/submitfile", method = {RequestMethod.POST}, consumes = {"application/x-www-form-urlencoded", "multipart/form-data"})
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public CommonResult submitProduct(@RequestParam(name = "userVOIdFile") Integer userid,@RequestParam(name = "uploadfile") MultipartFile file) throws BusinessException, IOException, NoSuchAlgorithmException {
        if (file == null) {
            log.error("submitProduct：上传文件失败！");
            throw new BusinessException(new BusinessException(EmBusinessError.UNKNOWN_ERROR, "上传文件失败！"));
        }
        System.out.println(file.getOriginalFilename());
        if(file.getOriginalFilename().contains("小黄鸡")){
            InputStream in = file.getInputStream();
            try {
                EasyExcel.read(in, QAExcel.class, new ExcelChickenListener(chickenDOMapper)).sheet().doRead();
                in.close();
            } catch (Exception e) {
                throw new BusinessException(new BusinessException(EmBusinessError.UNKNOWN_ERROR, "上传问答语料文件失败！"));
            }
            return CommonResult.create("成功！");
        }else{
            InputStream in = file.getInputStream();

            EasyExcel.read(in, TransportExcel.class, new ExceTransportListener(transportService, userid)).sheet().doRead();
            in.close();
            return CommonResult.create(null);
        }

    }


    @RequestMapping("/getdetail")
    @ResponseBody
    public CommonResult getDeatailById(@RequestParam(name="id")Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException{
        TransportModel materialInfoDO= transportService.getDetailById(id);
        return CommonResult.create(materialInfoDO);
    }

    @RequestMapping("/deletebyid")
    @ResponseBody
    public CommonResult deleteById(@RequestParam(name="id")Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException{
        transportService.deleteById(id);
        return CommonResult.create(null);
    }

}
